namespace Baddies
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Baddies.Events;
    using Baddies.GUI;
    using Baddies.InputDevices; 
    using Baddies.Nodes;
    using Baddies.Scenes; 
    using Baddies.Utils;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
                 
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        /// <summary>
        /// Graphics device manager.
        /// </summary>
        private GraphicsDeviceManager graphics;

        /// <summary>
        /// Sprite batch.
        /// </summary>
        private SpriteBatch spriteBatch;

        /// <summary>
        /// Node for the game tree.
        /// </summary>
        private Node root;

        /// <summary>
        /// Initializes a new instance of the Game1 class.
        /// </summary>
        public Game1()
        {
            this.graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            // TODO: Add your initialization logic here
            EventManager.GetEV.RegisterNewEvent("GetGameServices", true);
            EventManager.GetEV.Register("GetGameServices", new EventHandler(this.GetGameServices));

            EventManager.GetEV.RegisterNewEvent("GetGameGfxDevice", true);
            EventManager.GetEV.Register("GetGameGfxDevice", new EventHandler(this.GetGameGfxDevice));

            EventManager.GetEV.RegisterNewEvent("PrintTree", false);
            EventManager.GetEV.Register("PrintTree", this.PrintTree);

            this.root = new Scene();
            this.root.ParentRelativePos = false;
            this.root.Enter();

            BoxGUI box = new BoxGUI();
            box.Position = new Vector3(0, 0, 1);
            box.W = 20;
            box.H = 15;
            box.Name = "Box";
            box.ChangeBGColor(Color.Blue);

            BoxGUI box2 = new BoxGUI();
            box2.Position = new Vector3(6, 6, 2);
            box2.W = 20;
            box2.H = 15;
            box2.Name = "box2";
            box2.ChangeBGColor(Color.Brown);

            ConsoleGUI consoleGui = new ConsoleGUI();

            InputDevice inputDevice = new InputDevice();
            inputDevice.Position = new Vector3(0, 0, 1);
            inputDevice.Name = "iDevice";
            inputDevice.Add(0, box);
            inputDevice.Add(1, consoleGui);
            inputDevice.Add(2, box2);
            inputDevice.FocusedNode = consoleGui;

            Camera cam = new Camera(100, 100);
            cam.Position = new Vector3(-10, -10, 0);
            cam.Name = "Cam";

            Console console = new Console();
            
            this.root.Add(0, cam);
            this.root.Add(1, inputDevice);
            this.root.Add(2, console);                      
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            this.spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // TODO: Add your update logic here            
            base.Update(gameTime);
            
            this.root.NodeUpdate();
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            base.Draw(gameTime);

            this.spriteBatch.Begin();
            this.root.NodeDraw(this.spriteBatch);
            this.spriteBatch.End();
        }

        /// <summary>
        /// Delegate function for the GetGameServices event.
        /// </summary>
        /// <param name="sender">Sender of the request.</param>
        /// <param name="e">GetGameServicesArg parameter.</param>
        private void GetGameServices(object sender, EventArgs e)
        {
            ((GetGameServicesArg)e).Service = this.Services;
        }

        /// <summary>
        /// Delegate function for the GetGameGfxDevice event.
        /// </summary>
        /// <param name="sender">Sender of the request.</param>
        /// <param name="e">GetGameGfxDeviceArg parameter.</param>
        private void GetGameGfxDevice(object sender, EventArgs e)
        {
            ((GetGameGfxDeviceArg)e).GraphicsDevice = this.GraphicsDevice;
        }

        /// <summary>
        /// Delegate function for the PrintTree event.
        /// </summary>
        /// <param name="sender">Sender of the request.</param>
        /// <param name="e">No EventArgs parameter.</param>
        private void PrintTree(object sender, EventArgs e)
        {
            Log.F.Write(this.root.PrintSubtree());
        }
    }
}
